package com.castle.fortress.admin.member.account.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.account.entity.MemberAccountEntity;
import com.castle.fortress.admin.member.account.dto.MemberAccountDto;
import com.castle.fortress.admin.member.account.mapper.MemberAccountMapper;
import com.castle.fortress.admin.member.account.service.MemberAccountService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 会员账户表 服务实现类
 *
 * @author 
 * @since 2025-04-15
 */
@Service
public class MemberAccountServiceImpl extends ServiceImpl<MemberAccountMapper, MemberAccountEntity> implements MemberAccountService {
    /**
    * 获取查询条件
    * @param memberAccountDto
    * @return
    */
    public QueryWrapper<MemberAccountEntity> getWrapper(MemberAccountDto memberAccountDto){
        QueryWrapper<MemberAccountEntity> wrapper= new QueryWrapper();
        if(memberAccountDto != null){
            MemberAccountEntity memberAccountEntity = ConvertUtil.transformObj(memberAccountDto,MemberAccountEntity.class);
            wrapper.like(memberAccountEntity.getId() != null,"id",memberAccountEntity.getId());
            wrapper.like(memberAccountEntity.getMemberId() != null,"member_id",memberAccountEntity.getMemberId());
            wrapper.like(StrUtil.isNotEmpty(memberAccountEntity.getBalance()),"balance",memberAccountEntity.getBalance());
            wrapper.like(StrUtil.isNotEmpty(memberAccountEntity.getFrozen()),"frozen",memberAccountEntity.getFrozen());
            wrapper.like(memberAccountEntity.getStatus() != null,"status",memberAccountEntity.getStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<MemberAccountDto> pageMemberAccount(Page<MemberAccountDto> page, MemberAccountDto memberAccountDto) {
        QueryWrapper<MemberAccountEntity> wrapper = getWrapper(memberAccountDto);
        Page<MemberAccountEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberAccountEntity> memberAccountPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberAccountDto> pageDto = new Page(memberAccountPage.getCurrent(), memberAccountPage.getSize(),memberAccountPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberAccountPage.getRecords(),MemberAccountDto.class));
        return pageDto;
    }

    @Override
    public IPage<MemberAccountDto> pageMemberAccountExtends(Page<MemberAccountDto> page, MemberAccountDto memberAccountDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        MemberAccountEntity memberAccountEntity = ConvertUtil.transformObj(memberAccountDto,MemberAccountEntity.class);
        List<MemberAccountEntity> memberAccountList=baseMapper.extendsList(pageMap,memberAccountEntity);
        Long total = baseMapper.extendsCount(memberAccountEntity);
        Page<MemberAccountDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(memberAccountList,MemberAccountDto.class));
        return pageDto;
    }
    @Override
    public MemberAccountDto getByIdExtends(Long id){
        MemberAccountEntity  memberAccountEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(memberAccountEntity,MemberAccountDto.class);
    }

    @Override
    public List<MemberAccountDto> listMemberAccount(MemberAccountDto memberAccountDto){
        QueryWrapper<MemberAccountEntity> wrapper = getWrapper(memberAccountDto);
        List<MemberAccountEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberAccountDto.class);
    }
}

